package com.vdong.sharding.rule.table;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;


/**
 * 自定义分表逻辑
 */

@Slf4j
public class MyTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    /**
     * 自定义分表逻辑
     *
     * @param collection
     * @param preciseShardingValue
     * @return
     */
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long value = preciseShardingValue.getValue();
        long last4 = value % 10000;
        long tableIndex = (last4 / 2) % 2;
        String logicTableName = preciseShardingValue.getLogicTableName();
        String realTable = logicTableName + "_" + tableIndex;
        System.out.println("------------------分表-----------------");
        System.out.println("分片值:" + value + ",分配表:" + realTable);
        System.out.println("-------------------------------------");
        return realTable;
    }


}
